﻿using System;
using System.Windows;
using PasswordProvider.ViewModel.Properties;
using System.Windows.Forms;


namespace PasswordProvider.ViewModel.Service
{
    /// <summary>
    /// Simple dialog service for displaying message boxes and file dialogs.
    /// </summary>
    public static class DialogViewService
    {
        /// <summary>
        /// Show a message box. Returns true if OK or Yes were clicked.
        /// </summary>
        public static bool ShowMessage(object requester, string message, string caption, MessageBoxButton button)
        {
            if (message == null)
                throw new ArgumentNullException(message);

            if (caption == null)
                caption = Strings.MainWindow_Title;

            MessageBoxViewModel vm = new MessageBoxViewModel(caption, message, button);
            MessagingService.RequestShowMessageBox(requester, vm);            
            
            return vm.IsOk;
        }

        /// <summary>
        /// Show the Open File dialog. 
        /// </summary>
        /// <returns>The file name or null if action was canceled.</returns>
        public static string ShowOpenFileDialog(string title, string initialDirectory)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.DefaultExt = ".xml";
            dialog.Filter = "Password Provider File (*.xml)|*.xml|All Files (*.*)|*.*";
            dialog.InitialDirectory = initialDirectory;
            dialog.Title = title;
            dialog.SupportMultiDottedExtensions = true;
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                return dialog.FileName;
            }
            return null;
        }
        /// <summary>
        /// Show the Save File dialog.
        /// </summary>
        /// <returns>The file name or null if action was canceled.</returns>
        public static string ShowSaveFileDialog(string title, string initialDirectory, string defaultExt = ".xml", string filter = "Password Provider File (*.xml)|*.xml|All Files (*.*)|*.*")
        {
            SaveFileDialog dialog = new SaveFileDialog();
            
            dialog.DefaultExt = defaultExt;
            dialog.Filter = filter;
            dialog.InitialDirectory = initialDirectory;
            dialog.Title = title != null ? title : Strings.MainWindow_Title;
            dialog.SupportMultiDottedExtensions = true;
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                return dialog.FileName;
            }
            return null;
        }
    }
}
